package pro3;

import java.util.*;

/**
 * @version 1.0
 * @since:
 * @ClassName:DealCards
 * @author: Li Jiahui
 * @date: 2021-04-06 17:42
 * 使用集合实现斗地主游戏的部分功能，要求如下：
 *  （1）首先准备 54 张扑克牌并打乱顺序。
 *  （2）由三个玩家交替摸牌，每人 17 张扑克牌，最后三张留作底牌。
 *  （3）查看三个玩家手中的扑克牌和底牌。
 *  （4）其中玩家手中的扑克牌需要按照大小顺序打印，规则如下：手中扑克牌从大到小的摆放顺序：大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3
 */
public class DealCards {
    public static void main(String[] args) {
        //1.自定义大小王和13个数字，按大小顺序排序
        String[] value = { "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
        //2.自定义花色
        String[] type ={"黑桃", "红桃", "梅花", "方块"};
        //3.定义一个map，存放生成的扑克牌
        Map<Integer, String> cards = new HashMap<>();
        //4.定义keys用来存放扑克牌的牌号
        List<Integer> keys = new ArrayList<>();
        //4.先大小王放到map集合中
        cards.put(0,"大王");
        cards.put(1,"小王");
        keys.add(0);
        keys.add(1);
        for (int i = 0; i<13; i++){
            for (int j = 0; j<4; j++){
                int key = 2+i*4+j;
                keys.add(key);
                cards.put(key,(type[j]+value[i]));
                //System.out.print(key + "=" + type[i]+ value[j] + " ");
            }
        }
        //输出一副补克牌信息
        System.out.println("使用一副扑克牌：" + cards);
        //4.随机洗牌
        Collections.shuffle((List<?>) keys);
        //确认一下随机洗牌是否成功
        System.out.println("随机洗牌生成情况:" + keys);
        //分发扑克牌给三个玩家，自留三张底牌
        List<Integer> play1 = keys.subList(0,17);
        List<Integer> play2 = keys.subList(17,34);
        List<Integer> play3 = keys.subList(34,51);
        List<Integer> others = keys.subList(51,54);
        System.out.println("玩家1：");
        System.out.println(play1);
        for (int i = 0; i <= 16; i++) {
            System.out.print(cards.get(play1.get(i))+" ");
        }
        System.out.println();
        System.out.println("玩家2：");
        for (int i = 0; i < play2.size(); i++) {
            System.out.print(cards.get(play2.get(i))+" ");
        }
        System.out.println();
        System.out.println("玩家3：");
        for (int i = 0; i < play3.size(); i++) {
            System.out.print(cards.get(play3.get(i))+" ");
        }
        System.out.println();
        System.out.println("底牌：");
        for (int i = 0; i < others.size(); i++) {
            System.out.print(cards.get(others.get(i))+" ");
        }
        System.out.println();
        //对玩家手中的扑克牌排序并打印显示出来
        Collections.sort(play1);
        System.out.println("玩家1：");
        for (int i = 0; i < play1.size(); i++){
            System.out.print(cards.get(play1.get(i)) + " ");
        }
        System.out.println();
        Collections.sort(play2);
        System.out.println("玩家2：");
        for (int i = 0; i < play1.size(); i++){
            System.out.print(cards.get(play2.get(i)) + " ");
        }
        System.out.println();
        Collections.sort(play3);
        System.out.println("玩家3：");
        for (int i = 0; i < play1.size(); i++){
            System.out.print(cards.get(play3.get(i)) + " ");
        }

    }
}
